home *** CD-ROM | disk | FTP | other *** search
- /*
- ** MULTI.C
- **
- ** This example demonstrates the use of threads. One thread is started
- ** for each FTP account. The thread logs onto the FTP server and retrieves
- ** a list of files, then logs off.
- **
- ** Edit the FTP account information on line below (line 123) before compiling.
- **
- ** Must compile with -MT flag (Microsoft).
- **
- */
-
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <process.h>
- #include "fce.h"
-
- #define NBR_THREADS 10
-
- #define QUOTE 0x22
-
- typedef struct
- {int IsRunning;
- int ThreadID;
- char Srvr[40];
- char User[40];
- char Pass[40];
- char Temp[1024];
- long WorkCount;
- long WaitCount;
- } FtpParamType;
-
- static FtpParamType FtpParams[NBR_THREADS];
-
- /* FTP thread function */
-
- void FtpThread(PVOID DataPtr)
- {int Code;
- LPSTR Ptr1;
- LPSTR Ptr2;
- int ThreadID;
- FtpParamType *ParamPtr;
- char Temp[40];
- ParamPtr = (FtpParamType *) DataPtr;
- ParamPtr->IsRunning = TRUE;
- /* starting Ftp thread to check email status */
- ThreadID = ParamPtr->ThreadID;
- printf("Thread %d: Started.\n",ThreadID);
- /* define diagnostics log file */
- sprintf(Temp,"MULTI%1d.LOG",ParamPtr->ThreadID);
- fceSetString(ThreadID,FCE_SET_LOG_FILE, (LPSTR)Temp);
- /* connect to FTP server */
- printf("Thread %d: Connecting to Server %s for User %s\n",
- ThreadID, (LPSTR)&(ParamPtr->Srvr),(LPSTR)&(ParamPtr->User) );
- Code = fceConnect(ThreadID,
- (LPSTR)&(ParamPtr->Srvr), /* FTP Server */
- (LPSTR)&(ParamPtr->User), /* User */
- (LPSTR)&(ParamPtr->Pass)); /* Password */
- if(Code<0)
- {printf("Thread %d: Cannot connect to %s\n",
- ThreadID,(LPSTR)&(ParamPtr->Srvr));
- ParamPtr->IsRunning = FALSE;
- return;
- }
- Sleep(0);
- /* get list of filenames */
- Code = fceGetList(ThreadID,FCE_NAME_LIST,(LPSTR)ParamPtr->Temp,1024);
- if(Code<0)
- {printf("Thread %d: Error \n",ThreadID);
- ParamPtr->IsRunning = FALSE;
- fceClose(ThreadID);
- return;
- }
- printf("Thread %d: File list follows\r\n",ThreadID);
- Ptr1 = (LPSTR)ParamPtr->Temp;
- while(1)
- {Ptr2 = strchr(Ptr1,'\r');
- if(Ptr2==NULL) break;
- *Ptr2 = '\0';
- printf("Thread %d: %s\n", ThreadID,Ptr1);
- Sleep(0);
- Ptr1 = Ptr2 + 2;
- }
- fceClose(ThreadID);
- Sleep(0);
- /* thread has completed */
- ParamPtr->IsRunning = FALSE;
- printf("Thread %d: Completed.\n", ThreadID);
- }
-
- void AddUser(int Index, LPSTR Srvr, LPSTR User, LPSTR Pass)
- {strcpy((LPSTR)(&FtpParams[Index].Srvr), Srvr);
- strcpy((LPSTR)(&FtpParams[Index].User), User);
- strcpy((LPSTR)(&FtpParams[Index].Pass), Pass);
- FtpParams[Index].IsRunning = FALSE;
- FtpParams[Index].ThreadID = Index;
- FtpParams[Index].WorkCount = 0;
- FtpParams[Index].WaitCount = 0;
- }
-
- /*** MAIN ***/
-
- void main(int argc, char *argv[])
- {int i;
- int Version;
- int Running;
- unsigned long TimeStart;
- unsigned long TimeStop;
- int ThreadsToRun;
-
- if(argc!=1)
- {printf("Usage: MULTI\n");
- exit(1);
- }
-
- /* hard code one FTP setting per thread */
- /* 'ThreadsToRun' must be <= 'NBR_THEADS' */
-
- AddUser(0, "10.0.0.1", "mike", "mike");
- AddUser(1, "10.0.0.1", "pam", "pam");
-
- AddUser(2, "10.0.0.2", "mike", "mike");
- AddUser(3, "10.0.0.2", "pam", "pam");
- //AddUser(4, "10.0.0.2", "lauren", "lauren");
- //AddUser(5, "10.0.0.2", "bingo", "bingo");
-
- ThreadsToRun = 4; // Must be <= NBR_THREADS
-
- /* allocate channels (one per thread) */
- fceAttach(ThreadsToRun);
-
- /* display FCE parameters */
- Version = fceGetInteger(0,FCE_GET_VERSION);
- printf("FCE4C Version %1x.%1x.%1x\n",0x0f&(Version>>8),0x0f&(Version>>4),0x0f&Version);
-
- /* set internal SEE SleepTime value (default = 20) */
- fceSetInteger(0, FCE_SET_SLEEP_TIME, 100);
-
- /* start FTP threads */
- for(i=0;i<ThreadsToRun;i++)
- {
- _beginthread(FtpThread, 0, (void *)&FtpParams[i] );
- }
- Sleep(100);
- /* get start start time */
- TimeStart = GetCurrentTime();
-
- /* run until all threads are done */
- while(1)
- {/* are all threads done ? */
- Running = 0;
- for(i=0;i<ThreadsToRun;i++) if(FtpParams[i].IsRunning) Running++;
- if(Running==0)
- {TimeStop = GetCurrentTime();
- printf("All threads have terminated.\n");
- for(i=0;i<ThreadsToRun;i++)
- printf("Thread %d: WorkCount = %d; WaitCount = %d\n",
- i, FtpParams[i].WorkCount, FtpParams[i].WaitCount);
- printf("Total time = %d ms per user\n", (TimeStop - TimeStart) / ThreadsToRun);
- fceRelease();
- return;
- }
- Sleep(0);
- }
-
- } /* end main */
-
-